using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using BPMN.MainWindow;

namespace BPMN.MainWindow
{
    public partial class WindowMain : Form
    {
        
        #region Fields
        Canvas canvas;
        BPMNClass bpmn;
        string shapeTypeName;
        public UCPoolProperties poolProperties = null;
        public UCLaneProperties laneProperties = null;
        #endregion

        #region Properties
        public string ShapeTypeName
        {
            get
            {
                return shapeTypeName;
            }
            set
            {
                shapeTypeName = value;
            }
        }
        public BPMNClass BPMNClass
        {
            get
            {
                return bpmn;
            }
            set
            {
                bpmn = value;
            }
        }
        #endregion

        public WindowMain()
        {
            InitializeComponent();
            bpmn = new BPMNClass(new Size(800, 600));
            this.canvas = new Canvas(this);
            ContentPnl.Controls.Add(this.canvas);
            canvas.Size = new Size(800, 600);
            canvas.BorderStyle = BorderStyle.FixedSingle;
            canvas.Dock = DockStyle.Fill;
            this.Location = new Point(10, 10);
            this.Location = new Point(11, 11);
            poolProperties = new UCPoolProperties();
            poolProperties.Dock = DockStyle.Fill;
            poolProperties.Visible = false;
            splitContainer1.Panel2.Controls.Add(poolProperties);

            laneProperties = new UCLaneProperties();
            laneProperties.Dock = DockStyle.Fill;
            laneProperties.Visible = false;
            splitContainer1.Panel2.Controls.Add(laneProperties);

            this.myNavigationTree1.SetParent(this);
            poolProperties.SetParent(this);
            laneProperties.SetParent(this);

            initializeToolbarMenu();
        }
        protected void initializeToolbarMenu()
        {
            toolbarMenu1.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\select.gif"), "Select", "select"));
            toolbarMenu1.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\resize.gif"), "Resize", "resize"));

            toolbarMenu2.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\pool.gif"), "Pool", "pool"));
            toolbarMenu2.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\lane.gif"), "Lane", "lane"));

            toolbarMenu3.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\messageSE.gif"), "Message", "EventStartMessage"));
            toolbarMenu3.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\timerSE.gif"), "Timer", "EventStartTimer"));
            toolbarMenu3.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\ruleSE.gif"), "Rule", "EventStartRule"));
            toolbarMenu3.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\linkSE.gif"), "Link", "EventStartLink"));
            toolbarMenu3.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\multipleSE.gif"), "Multiple", "EventStartMultiple"));

            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\messageIE.gif"), "Message", "EventIntermediateMessage"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\timerIE.gif"), "Timer", "EventIntermediateTimer"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\errorIE.gif"), "Error", "EventIntermediateError"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\cancelIE.gif"), "Cancel", "EventIntermediateCancel"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\compensationIE.gif"), "Compensation", "EventIntermediateCompensation"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\ruleIE.gif"), "Rule", "EventIntermediateRule"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\linkIE.gif"), "Link", "EventIntermediateLink"));
            toolbarMenu4.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\multipleIE.gif"), "Multiple", "EventIntermediateMultiple"));

            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\messageEE.gif"), "Message", "EventEndMessage"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\errorEE.gif"), "Error", "EventEndError"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\cancelEE.gif"), "Cancel", "EventEndCancel"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\compensationEE.gif"), "Compensation", "EventEndCompensation"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\linkEE.gif"), "Link", "EventEndLink"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\multipleeE.gif"), "Multiple", "EventEndMultiple"));
            toolbarMenu5.AddImageItem(new BPMN.Controls.ImageItem(Image.FromFile(@"..\images\terminateEE.gif"), "Terminate", "EventEndTerminate"));

            toolbarMenu1.updateBoundingBoxes();
            toolbarMenu2.updateBoundingBoxes();
            toolbarMenu3.updateBoundingBoxes();
            toolbarMenu4.updateBoundingBoxes();
            toolbarMenu5.updateBoundingBoxes();
        }
        public void updateCanvasParameters()
        {
            if (canvas != null)
            {
                canvas.GlobalOffset = ContentPnl.Location;
                Point point = this.PointToScreen(new Point(0, 0));
                canvas.GlobalOffset = new Point(canvas.GlobalOffset.X + point.X, canvas.GlobalOffset.Y + point.Y);
            }
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            updateCanvasParameters();
            if (canvas != null)
            {
                canvas.Refresh();
            }
        }
        protected override void OnMove(EventArgs e)
        {
            updateCanvasParameters();
        }



        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            canvas.State = State.Building;

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            canvas.State = State.Building;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            canvas.State = State.Building;
        }

        private void toolbarMenu1_MenuItemDoubleClick(object sender, string id)
        {
            Debug.WriteLine("doubleClick" + id);
        }

        private void toolbarMenu1_MenuItemSelectionChanged(object sender, string id)
        {
            switch (id)
            {
                case "select":
                    canvas.State = State.Selecting;
                    break;
                case "resize":
                    canvas.State = State.Resizing;
                    canvas.Refresh();
                    break;
                case "draw":
                    for (int i = 0; i < bpmn.Pools.Count; i++)
                    {
                        if (bpmn.Pools[i].State == FigureState.Resizing)
                        {
                            bpmn.Pools[i].State = FigureState.Selected;
                        }
                    }
                    canvas.State = State.Drawing;
                    canvas.Refresh();
                    break;
            }
        }

        private void toolbarMenu2_MenuItemDoubleClick(object sender, string id)
        {
            switch (id)
            {
                case "pool":
                    canvas.State = State.Drawing;
                    PointF poolLocation = new PointF(Variables.XOffset, (bpmn.Pools.Count + 1) * Variables.YOffset);
                    for (int i = 0; i < bpmn.Pools.Count; i++)
                    {
                        poolLocation = new PointF(poolLocation.X, poolLocation.Y + bpmn.Pools[i].Size.Height);
                    }
                    Pool pool = new Pool(poolLocation, new SizeF(bpmn.Size.Width - 2 * Variables.XOffset, Variables.DefaultHeight));
                    Random r = new Random();
                    pool.Name = "Pool" + bpmn.Pools.Count + r.Next(1000);
                    pool.BoundingBox = new RectangleF(pool.Location, pool.Size);
                    bpmn.Pools.Add(pool);
                    FigureUpdateEventArgs args = new FigureUpdateEventArgs();
                    args.Type = FigureEventType.New;
                    args.NewFigure = new FigureParentChild(pool.Name, null, FigureType.Pool);
                    EventManagement.SendEventSignal(null, args);
                    break;

                case "lane":
                    if (bpmn.CurrentPool != null)
                    {
                        for (int i = bpmn.CurrentPoolIndex + 1; i < bpmn.Pools.Count; i++)
                        {
                            bpmn.Pools[i].Location = new PointF(bpmn.Pools[i].Location.X, bpmn.Pools[i].Location.Y + Variables.DefaultHeight);
                        }
                        r = new Random();
                        Lane lane = new Lane("Lane" + bpmn.CurrentPool.Lanes.Count + r.Next(1000), bpmn.CurrentPool, new PointF(bpmn.CurrentPool.Location.X + Variables.PoolTitleWidth, bpmn.CurrentPool.Location.Y + bpmn.CurrentPool.Size.Height), new SizeF(bpmn.CurrentPool.Size.Width - Variables.PoolTitleWidth, Variables.DefaultHeight));
                        bpmn.CurrentPool.Lanes.Add(lane);
                        bpmn.CurrentPool.Size = new SizeF(bpmn.CurrentPool.Size.Width, bpmn.CurrentPool.Size.Height + Variables.DefaultHeight);
                        FigureParentChild fpc = new FigureParentChild(lane.Name, null, FigureType.Lane);
                        EventManagement.SendEventSignal(canvas, new FigureUpdateEventArgs(fpc, fpc, FigureEventType.New, FigureType.Lane));
                        canvas.updateBPMN();
                        this.Refresh();
                    }
                    break;

            }
        }

        private void toolbarMenu3_MenuItemSelectionChanged(object sender, string id)
        {
            canvas.State = State.Building;
            shapeTypeName = id;
        }



    }
    public enum EventType
    {
        StartEvent,
        IntermediateEvent,
        EndEvent
    }
    public enum GatewaysType
    {
        Xor,
        Or,
        Complex
    }
}